package com.clusterswisdom.horn.api.ioc.api;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Element;

import com.clusterswisdom.horn.api.ioc.HornAction;

public class ApiConfig extends HashMap<String, String> implements Serializable {

	private static final long serialVersionUID = 5426228242334366832L;

	private ApiServiceConfig service;
	
	private String name;
	
	private String title;
	
	private String cron;
	
	private String data;
	
	private String application;
	
	private boolean log;
	
	private HornAction action;
	
	private Map<String, String> attribute;
	
	private RequestConfig request;
	
	private ResponseConfig response;
	
	public ApiConfig(ApiServiceConfig service, Element element) {
		parse(element);
		this.service = service;
	}
	
	public String getAttribute(String attr) {
		return attribute.get(attr);
	}
	
	public ApiServiceConfig getService() {
		return service;
	}

	public void setService(ApiServiceConfig service) {
		this.service = service;
	}

	public String getName() {
		return name;
	}

	public String getApplication() {
		return application;
	}

	public boolean isLog() {
		return log;
	}

	public String getTitle() {
		return title;
	}

	public String getCron() {
		return cron;
	}

	public String getData() {
		return data;
	}
	
	public HornAction getAction() {
		return action;
	}

	public Map<String, String> getAttribute() {
		return attribute;
	}

	public ResponseConfig getResponse() {
		return response;
	}

	public RequestConfig getRequest() {
		return request;
	}

	private void parse(Element root) {
		this.name = root.attributeValue("name");
		this.title = root.attributeValue("title");
		this.cron = root.attributeValue("cron");
		this.data = root.attributeValue("data");
		this.application = root.attributeValue("application");
		this.log = !"false".equalsIgnoreCase(root.attributeValue("log"));
		
		String actionClass = root.attributeValue("action");
		if (actionClass != null) {
			try {
				this.action = (HornAction)Thread.currentThread().getContextClassLoader().loadClass(actionClass).newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		this.attribute =  getAttribute(root.element("attribute"));
		this.request = getRequest(root.element("request"));
		
		Element responseElement = root.element("response");
		if (responseElement != null) {
			this.response = new ResponseConfig(responseElement);
		}
	}
	
	@SuppressWarnings("unchecked")
	private Map<String, String> getAttribute(Element element) {
		if (element == null) {
			return new HashMap<String, String>();
		}
		
	    Iterator<Element> attrIterator = element.elementIterator("attr");
		Map<String, String> attribute = new HashMap<String, String>();
		while (attrIterator.hasNext()) {
	    	Element attrElement = attrIterator.next();
	    	attribute.put(attrElement.attributeValue("name"), attrElement.attributeValue("value"));
	    }

		return attribute;
	}
	
	private RequestConfig getRequest(Element element) {
		if (element == null) {
			return null;
		}
		
		return new RequestConfig(element);
	}
}